package ro.gillardon.business.priceservice.impl.hotel;

import java.util.List;

import javax.ejb.Stateless;

import ro.gillardon.business.exception.BusinessException;
import ro.gillardon.business.priceservice.HotelPriceService;
import ro.gillardon.commons.Conditions;
import ro.gillardon.commons.enums.City;
import ro.gillardon.commons.enums.Hotel;
import ro.gillardon.commons.hotel.HotelConditions;
import ro.gillardon.commons.hotel.HotelDetailsWrapper;
import ro.gillardon.persistence.DAOFactory;
import ro.gillardon.persistence.HotelDetailsDAO;
import ro.gillardon.persistence.exception.PersistenceException;

@Stateless
public class HotelPriceServiceEJB implements HotelPriceService {

  /**
   * Instantiates a new HotelPriceServiceEJB.
   */
  public HotelPriceServiceEJB() {
    // constructor without parameters
  }

  private HotelConditions checkForCorrectType(final Conditions conditions)
    throws BusinessException {
    if (!(conditions instanceof HotelConditions)) {
      throw new BusinessException();
    }
    return (HotelConditions) conditions;
  }

  @Override
  public HotelDetailsWrapper getOffers(final Conditions conditions) throws
    BusinessException {

    try {
      HotelConditions hotelConditions = checkForCorrectType(conditions);
      HotelDetailsDAO hotelDetailsDAO = DAOFactory.getDAOFactory().getHotelDetailsDAO();
      return hotelDetailsDAO.getLatestHotelOffer(hotelConditions.getHotel());
    } catch (PersistenceException e) {
      throw new BusinessException(e.getMessage(), e);
    }
  }

  @Override
  public List<Hotel> getHotelsForLocation(final City city) throws
  BusinessException {
    try {
      HotelDetailsDAO hotelDetailsDAO = DAOFactory.getDAOFactory().getHotelDetailsDAO();
      return hotelDetailsDAO.getAllHotelsFromLocation(city);

    } catch (PersistenceException e) {
      throw new BusinessException(e.getMessage(), e);
    }
  }

  @Override
  public List<City> getAllLocations() throws BusinessException {
    try {
      HotelDetailsDAO hotelDetailsDAO = DAOFactory.getDAOFactory().getHotelDetailsDAO();
      return hotelDetailsDAO.getAllLocations();

    } catch (PersistenceException e) {
      throw new BusinessException(e.getMessage(), e);
    }
  }

}
